const monthDays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

export function getMonthEndDay(year_month: string) {
  const [year, month] = year_month.split("-");
  const day = monthDays[parseInt(month)];
  return year + "-" + month + "-" + day;
}

export function getDateFormat(dateStr?: string) {
  let date: Date;
  if (!dateStr) {
    date = new Date();
  } else {
    dateStr = dateStr.replace(/-/g, "/");
    date = new Date(dateStr);
  }
  return date;
}

export function formatToDay(dateStr?: string) {
  const date = getDateFormat(dateStr);
  const day = date.getDate().toString().padStart(2, "0");
  const month = (date.getMonth() + 1).toString().padStart(2, "0");
  const year = date.getFullYear();
  return year + "-" + month + "-" + day;
}

export function formatToMinute(dateStr?: string) {
  const date = getDateFormat(dateStr);
  let year = date.getFullYear();
  let month = (date.getMonth() + 1).toString().padStart(2, "0");
  let day = date.getDate().toString().padStart(2, "0");
  let hours = date.getHours().toString().padStart(2, "0");
  let minutes = date.getMinutes().toString().padStart(2, "0");
  return `${year}-${month}-${day} ${hours}:${minutes}`;
}

export function formatDateToMinute(date: Date) {
  let year = date.getFullYear();
  let month = (date.getMonth() + 1).toString().padStart(2, "0");
  let day = date.getDate().toString().padStart(2, "0");
  let hours = date.getHours().toString().padStart(2, "0");
  let minutes = date.getMinutes().toString().padStart(2, "0");
  return `${year}/${month}/${day} ${hours}:${minutes}`;
}

export function formatHourMinute(dateStr?: string) {
  const date = getDateFormat(dateStr);
  let hours = date.getHours().toString().padStart(2, "0");
  let minutes = date.getMinutes().toString().padStart(2, "0");
  return `${hours}:${minutes}`;
}

export function formatHourMinuteSeconds(dateStr?: string) {
  const date = getDateFormat(dateStr);
  let hours = date.getHours().toString().padStart(2, "0");
  let minutes = date.getMinutes().toString().padStart(2, "0");
  let seconds = date.getSeconds().toString().padStart(2, "0");
  return `${hours}:${minutes}:${seconds}`;
}
